<?php
namespace Admin\Controller;
use Think\Controller;
class CateController extends Controller {
    public function lst(){
        $cate=D('cate');//实例化数据库
        $cates=$cate->select();
        $this->assign('cates',$cates);//分配数据,定义前端中volist的的值遍历，assign('cates',$cates)中参数名要与前端<volist name="" id="">的name保持一致
        $this->display();
    }



    public function add(){
    	$cate=D('cate');//实例化数据库
    	if (IS_POST) {
    		$data['cate_name']=I('cate_name');
    		//dump($data);die();
        
            if ($cate->create($data)) {
                if ($cate->add()) {
                    $this->success('新增栏目成功',U(lst));
                }else{
                    $this->error('新增栏目失败');
                }
            }else{
                $this->error($cate->getError());
            }
            return;
    	}

    	$this->display();
    }



    public function del(){
        $cate=D('cate');//实例化数据库
        if ($cate->delete(I('id'))) {
            $this->success("删除栏目成功",U(lst));
        }else{
            $this->error("删除栏目失败");
        }

        /*$this->display();*/
    }

    public function edit(){
        $cate=D('cate');//实例化数据库
        if (IS_POST) {
            $data['cate_id']=I('cate_id');
            $data['cate_name']=I('cate_name');
            //dump($data);die();
        
            if ($cate->create($data)) {
                if ($cate->save()) {
                    $this->success('修改栏目成功',U(lst));
                }else{
                    $this->error('修改栏目失败');
                }
            }else{
                $this->error($cate->getError());
            }
            return;
        }
        $cateid=I('id');
        $caters=$cate->find($cateid);
        $this->assign('caters',$caters);
        $this->display();
    }
}